<script setup>
import {nextTick, onMounted, reactive, ref} from 'vue';
import router from "@/router/index.js";
import {ElMessage} from "element-plus";
import {DrawControl, DrawScene, OperateEventType} from 'bmap-draw';
import qs from 'qs';
import axios from 'axios';

const dialogVisible = ref(false);
const geofence = ref({name: '', position: {}});
let map = reactive(null);


// const saveGeofence = ref({});

const saveGeofence =async () => {
  if (!geofence.value.name || !geofence.value.name.trim()) {
    ElMessage.error("围栏名称不能为空! ！")
    return;
  }
  geofence.value.position = JSON.stringify(geofence.value.position)
  let data = qs.stringify(geofence.value);
  const {data:{code,msg}} = await axios.post(BASE_URL+"/v1/geofence/save",data);
  if(code === 2000){
    ElMessage.success("添加成功！")
    closeDialog();
    router.push('/geofence');
  }else{
    ElMessage.error(msg);
  }
};
const closeDialog = () => {
  dialogVisible.value = false;
  map.clearOverlays();
}
onMounted(() => {
  nextTick(() => {
    map = new BMapGL.Map("mapContainer");
    let point = new BMapGL.Point(116.4074, 39.924);
    map.centerAndZoom(point, 15);

    // 添加鼠标绘制控件
    // 1.创建绘制场景类，需要导入！此类为其它鼠标绘制的基类，参数为刚刚创建好的地图对象
    const scene = new DrawScene(map);
    // 2.创建鼠标绘制控件对象，基于上面的绘制场景，设置自己的绘制项
    const drawContrl = new DrawControl(scene, {
      enableTips: true, // 对于每一个绘制项的提示，默认关闭
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });


    // 监听绘制事件
    scene.addEventListener(OperateEventType.COMPLETE, (event) => {
      let overlay = event.target.overlay;
      dialogVisible.value = true;
      if (overlay instanceof BMapGL.Circle) {
        geofence.value.position.type = 'circle';
        geofence.value.position.radius = overlay.getRadius();

        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;

      } else {
        let recPoints = [];
        for (let i = 0; i < 4; i++) {
          recPoints.push(overlay.points[i].latLng.lng + "-" + overlay.points[i].latLng.lat);
        }
        geofence.value.position.type = 'rectangle';
        geofence.value.position.recPonits = recPoints.join(",");


      }
    })
    map.addControl(drawContrl);
  })
})


</script>

<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <el-button type="primary" @click="router.push('/geofence')" style="float:right;margin-top:13px;">返回</el-button>
  </div>

  <!--  地图容器-->
  <div id="mapContainer" style="height: 84vh;width:100%"></div>
  <!--  电子围栏弹窗-->
  <el-dialog v-model="dialogVisible" title="围栏信息">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input v-model="geofence.name" placeholder="请输入围栏名称"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>